﻿
Option Strict On

Imports System.Net
Imports System.Net.Sockets
Imports osi.root.constants
Imports osi.root.delegates
Imports osi.root.utils
Imports osi.root.formation
Imports osi.root.procedure
Imports osi.service.argument
Imports osi.service.commander
Imports osi.service.convertor
Imports osi.service.device
Imports osi.service.tcp.constants

Partial Public Class powerpoint
    Shared Sub New()
        assert(IPEndPoint.MinPort >= 0 AndAlso IPEndPoint.MaxPort <= max_uint16)
    End Sub

    Public Shared Function create(ByVal v As var,
                                  ByRef o As powerpoint) As Boolean
        Const p_is_outgoing As String = "is-outgoing"
        Const p_host As String = "host"
        Const p_port As String = "port"
        Const p_token As String = "token"
        Const p_max_connecting As String = "max-connecting"
        Const p_max_connected As String = "max-connected"
        Const p_no_delay As String = "no-delay"
        Const p_connecting_timeout_ms As String = "connecting-timeout-ms"
        Const p_response_timeout_ms As String = "response-timeout-ms"
        Const p_send_rate_sec As String = "send-rate-sec"
        Const p_receive_rate_sec As String = "receive-rate-sec"
        Const p_max_lifetime_ms As String = "max-lifetime-ms"
        Const p_ipv4 As String = "ipv4"
        Const p_enable_keepalive As String = "enable-keepalive"
        Const p_first_keepalive_ms As String = "first-keepalive-ms"
        Const p_keepalive_interval_ms As String = "keepalive-interval-ms"
        If v Is Nothing Then
            Return False
        Else
            v.bind(p_is_outgoing,
                   p_host,
                   p_port,
                   p_token,
                   p_max_connecting,
                   p_max_connected,
                   p_no_delay,
                   p_connecting_timeout_ms,
                   p_response_timeout_ms,
                   p_send_rate_sec,
                   p_receive_rate_sec,
                   p_max_lifetime_ms,
                   p_ipv4,
                   p_enable_keepalive,
                   p_first_keepalive_ms,
                   p_keepalive_interval_ms)
            Return create(v(p_token),
                          v(p_host),
                          v(p_port),
                          v(p_ipv4),
                          v(p_connecting_timeout_ms),
                          v(p_send_rate_sec),
                          v(p_response_timeout_ms),
                          v(p_receive_rate_sec),
                          v(p_max_connecting),
                          v(p_max_connected),
                          v(p_no_delay),
                          v(p_max_lifetime_ms),
                          v(p_is_outgoing),
                          v(p_enable_keepalive),
                          v(p_first_keepalive_ms),
                          v(p_keepalive_interval_ms),
                          o,
                          v)
        End If
    End Function

    Public Shared Function create(ByVal v As var) As powerpoint
        Dim o As powerpoint = Nothing
        assert(create(v, o))
        Return o
    End Function

    Public Shared Function create_as_block_device_pool(
                               ByVal v As var,
                               ByRef o As new_device_event_isync_device_pool(Of block)) As Boolean
        Dim p As powerpoint = Nothing
        If create(v, p) Then
            o = p.block_device_pool
            Return True
        Else
            Return False
        End If
    End Function

    Public Shared Function create_as_herald_device_pool(
                               ByVal v As var,
                               ByRef o As new_device_event_isync_device_pool(Of herald)) As Boolean
        Dim p As powerpoint = Nothing
        If create(v, p) Then
            o = p.herald_device_pool
            Return True
        Else
            Return False
        End If
    End Function

    Public Shared Function create(ByVal token As String,
                                  ByVal host_or_ip As String,
                                  ByVal port As UInt16,
                                  ByVal ipv4 As Boolean,
                                  ByVal connecting_timeout_ms As Int64,
                                  ByVal send_rate_sec As UInt32,
                                  ByVal response_timeout_ms As Int64,
                                  ByVal receive_rate_sec As UInt32,
                                  ByVal max_connecting As UInt32,
                                  ByVal max_connected As UInt32,
                                  ByVal no_delay As Boolean,
                                  ByVal max_lifetime_ms As Int64,
                                  ByVal is_outgoing As Boolean,
                                  ByVal enable_keepalive As Boolean,
                                  ByVal first_keepalive_ms As UInt32,
                                  ByVal keepalive_interval_ms As UInt32,
                                  ByRef r As powerpoint,
                                  Optional ByVal v As var = Nothing) As Boolean
        If max_connecting = 0 OrElse
           max_connected = 0 Then
            Return False
        ElseIf Not (is_outgoing Xor String.IsNullOrEmpty(host_or_ip)) Then
            Return False
        Else
            If max_connecting > max_connected Then
                max_connecting = max_connected
            End If
            r = New powerpoint(token,
                               host_or_ip,
                               port,
                               ipv4,
                               connecting_timeout_ms,
                               send_rate_sec,
                               response_timeout_ms,
                               receive_rate_sec,
                               max_connecting,
                               max_connected,
                               no_delay,
                               max_lifetime_ms,
                               is_outgoing,
                               enable_keepalive,
                               first_keepalive_ms,
                               keepalive_interval_ms,
                               v)
            closer.watch(r)
            If Not is_outgoing Then
                accepter.listen(r)
            End If
            Return True
        End If
    End Function

    Public Shared Function create(ByVal token As String,
                                  ByVal endpoint As IPEndPoint,
                                  ByVal connecting_timeout_ms As Int64,
                                  ByVal send_rate_sec As UInt32,
                                  ByVal response_timeout_ms As Int64,
                                  ByVal receive_rate_sec As UInt32,
                                  ByVal max_connecting As UInt32,
                                  ByVal max_connected As UInt32,
                                  ByVal no_delay As Boolean,
                                  ByVal max_lifetime_ms As Int64,
                                  ByVal is_outgoing As Boolean,
                                  ByVal enable_keepalive As Boolean,
                                  ByVal first_keepalive_ms As UInt32,
                                  ByVal keepalive_interval_ms As UInt32,
                                  ByRef r As powerpoint) As Boolean
        If endpoint Is Nothing Then
            Return False
        ElseIf Not is_outgoing AndAlso
               Not endpoint.Address().Equals(IPAddress.Any) AndAlso
               Not endpoint.Address().Equals(IPAddress.IPv6Any) Then
            Return False
        Else
            Return create(token,
                          If(is_outgoing, Convert.ToString(endpoint.Address()), Nothing),
                          CUShort(endpoint.Port()),
                          (endpoint.AddressFamily() = Sockets.AddressFamily.InterNetwork),
                          connecting_timeout_ms,
                          send_rate_sec,
                          response_timeout_ms,
                          receive_rate_sec,
                          max_connecting,
                          max_connected,
                          no_delay,
                          max_lifetime_ms,
                          is_outgoing,
                          enable_keepalive,
                          first_keepalive_ms,
                          keepalive_interval_ms,
                          r)
        End If
    End Function

    Public Shared Function create(ByVal token As String,
                                  ByVal endpoint As IPEndPoint,
                                  ByVal connecting_timeout_ms As Int64,
                                  ByVal send_rate_sec As UInt32,
                                  ByVal response_timeout_ms As Int64,
                                  ByVal receive_rate_sec As UInt32,
                                  ByVal max_connecting As UInt32,
                                  ByVal max_connected As UInt32,
                                  ByVal no_delay As Boolean,
                                  ByVal max_lifetime_ms As Int64,
                                  ByVal is_outgoing As Boolean,
                                  ByVal enable_keepalive As Boolean,
                                  ByVal first_keepalive_ms As UInt32,
                                  ByVal keepalive_interval_ms As UInt32) As powerpoint
        Dim r As powerpoint = Nothing
        assert(create(token,
                      endpoint,
                      connecting_timeout_ms,
                      send_rate_sec,
                      response_timeout_ms,
                      receive_rate_sec,
                      max_connecting,
                      max_connected,
                      no_delay,
                      max_lifetime_ms,
                      is_outgoing,
                      enable_keepalive,
                      first_keepalive_ms,
                      keepalive_interval_ms,
                      r))
        Return r
    End Function

    Public Shared Function create(ByVal token As String,
                                  ByVal port As UInt16,
                                  ByRef r As powerpoint,
                                  Optional ByVal send_rate_sec As UInt32 = _
                                        default_value.send_rate_sec,
                                  Optional ByVal response_timeout_ms As Int64 = _
                                        default_value.response_timeout_ms,
                                  Optional ByVal receive_rate_sec As UInt32 = _
                                        default_value.receive_rate_sec,
                                  Optional ByVal max_connecting As UInt32 = _
                                        default_value.incoming.max_connecting,
                                  Optional ByVal max_connected As UInt32 = _
                                        default_value.incoming.max_connected,
                                  Optional ByVal no_delay As Boolean = _
                                        default_value.no_delay,
                                  Optional ByVal max_lifetime_ms As Int64 = _
                                        default_value.incoming.max_lifetime_ms,
                                  Optional ByVal enable_keepalive As Boolean = _
                                        default_value.enable_keepalive,
                                  Optional ByVal first_keepalive_ms As UInt32 = _
                                        default_value.first_keepalive_ms,
                                  Optional ByVal keepalive_interval_ms As UInt32 = _
                                        default_value.keepalive_interval_ms) _
                                 As Boolean
        Return create(token,
                      Nothing,
                      port,
                      True,
                      npos,
                      send_rate_sec,
                      response_timeout_ms,
                      receive_rate_sec,
                      max_connecting,
                      max_connected,
                      no_delay,
                      max_lifetime_ms,
                      False,
                      enable_keepalive,
                      first_keepalive_ms,
                      keepalive_interval_ms,
                      r)
    End Function

    Public Shared Function create(ByVal token As String,
                                  ByVal port As UInt16,
                                  Optional ByVal send_rate_sec As UInt32 = _
                                        default_value.send_rate_sec,
                                  Optional ByVal response_timeout_ms As Int64 = _
                                        default_value.response_timeout_ms,
                                  Optional ByVal receive_rate_sec As UInt32 = _
                                        default_value.receive_rate_sec,
                                  Optional ByVal max_connecting As UInt32 = _
                                        default_value.incoming.max_connecting,
                                  Optional ByVal max_connected As UInt32 = _
                                        default_value.incoming.max_connected,
                                  Optional ByVal no_delay As Boolean = _
                                        default_value.no_delay,
                                  Optional ByVal max_lifetime_ms As Int64 = _
                                        default_value.incoming.max_lifetime_ms,
                                  Optional ByVal enable_keepalive As Boolean = _
                                        default_value.enable_keepalive,
                                  Optional ByVal first_keepalive_ms As UInt32 = _
                                        default_value.first_keepalive_ms,
                                  Optional ByVal keepalive_interval_ms As UInt32 = _
                                        default_value.keepalive_interval_ms) _
                                 As powerpoint
        Dim r As powerpoint = Nothing
        assert(create(token,
                      port,
                      r,
                      send_rate_sec,
                      response_timeout_ms,
                      receive_rate_sec,
                      max_connecting,
                      max_connected,
                      no_delay,
                      max_lifetime_ms,
                      enable_keepalive,
                      first_keepalive_ms,
                      keepalive_interval_ms))
        Return r
    End Function

    Public Shared Function create(ByVal token As String,
                                  ByVal remote_endpoint As IPEndPoint,
                                  ByRef r As powerpoint,
                                  Optional ByVal connecting_timeout_ms As Int64 = _
                                        default_value.outgoing.connecting_timeout_ms,
                                  Optional ByVal send_rate_sec As UInt32 = _
                                        default_value.send_rate_sec,
                                  Optional ByVal response_timeout_ms As Int64 = _
                                        default_value.response_timeout_ms,
                                  Optional ByVal receive_rate_sec As UInt32 = _
                                        default_value.receive_rate_sec,
                                  Optional ByVal max_connecting As UInt32 = _
                                        default_value.outgoing.max_connecting,
                                  Optional ByVal max_connected As UInt32 = _
                                        default_value.outgoing.max_connected,
                                  Optional ByVal no_delay As Boolean = _
                                        default_value.no_delay,
                                  Optional ByVal max_lifetime_ms As Int64 = _
                                        default_value.outgoing.max_lifetime_ms,
                                  Optional ByVal enable_keepalive As Boolean = _
                                        default_value.enable_keepalive,
                                  Optional ByVal first_keepalive_ms As UInt32 = _
                                        default_value.first_keepalive_ms,
                                  Optional ByVal keepalive_interval_ms As UInt32 = _
                                        default_value.keepalive_interval_ms) _
                                 As Boolean
        Return create(token,
                      remote_endpoint,
                      connecting_timeout_ms,
                      send_rate_sec,
                      response_timeout_ms,
                      receive_rate_sec,
                      max_connecting,
                      max_connected,
                      no_delay,
                      max_lifetime_ms,
                      True,
                      enable_keepalive,
                      first_keepalive_ms,
                      keepalive_interval_ms,
                      r)
    End Function

    Public Shared Function create(ByVal token As String,
                                  ByVal remote_endpoint As IPEndPoint,
                                  Optional ByVal connecting_timeout_ms As Int64 = _
                                        default_value.outgoing.connecting_timeout_ms,
                                  Optional ByVal send_rate_sec As UInt32 = _
                                        default_value.send_rate_sec,
                                  Optional ByVal response_timeout_ms As Int64 = _
                                        default_value.response_timeout_ms,
                                  Optional ByVal receive_rate_sec As UInt32 = _
                                        default_value.receive_rate_sec,
                                  Optional ByVal max_connecting As UInt32 = _
                                        default_value.outgoing.max_connecting,
                                  Optional ByVal max_connected As UInt32 = _
                                        default_value.outgoing.max_connected,
                                  Optional ByVal no_delay As Boolean = _
                                        default_value.no_delay,
                                  Optional ByVal max_lifetime_ms As Int64 = _
                                        default_value.outgoing.max_lifetime_ms,
                                  Optional ByVal enable_keepalive As Boolean = _
                                        default_value.enable_keepalive,
                                  Optional ByVal first_keepalive_ms As UInt32 = _
                                        default_value.first_keepalive_ms, _
                                  Optional ByVal keepalive_interval_ms As UInt32 = _
                                        default_value.keepalive_interval_ms) _
                                 As powerpoint
        Dim r As powerpoint = Nothing
        assert(create(token,
                      remote_endpoint,
                      r,
                      connecting_timeout_ms,
                      send_rate_sec,
                      response_timeout_ms,
                      receive_rate_sec,
                      max_connecting,
                      max_connected,
                      no_delay,
                      max_lifetime_ms,
                      enable_keepalive,
                      first_keepalive_ms,
                      keepalive_interval_ms))
        Return r
    End Function

    Public Shared Function create(ByVal token As String,
                                  ByVal remote_address As String,
                                  ByVal remote_port As UInt16,
                                  ByRef r As powerpoint,
                                  Optional ByVal connecting_timeout_ms As Int64 = _
                                        default_value.outgoing.connecting_timeout_ms,
                                  Optional ByVal send_rate_sec As UInt32 = _
                                        default_value.send_rate_sec,
                                  Optional ByVal response_timeout_ms As Int64 = _
                                        default_value.response_timeout_ms,
                                  Optional ByVal receive_rate_sec As UInt32 = _
                                        default_value.receive_rate_sec,
                                  Optional ByVal max_connecting As UInt32 = _
                                        default_value.outgoing.max_connecting,
                                  Optional ByVal max_connected As UInt32 = _
                                        default_value.outgoing.max_connected,
                                  Optional ByVal no_delay As Boolean = _
                                        default_value.no_delay,
                                  Optional ByVal max_lifetime_ms As Int64 = _
                                        default_value.outgoing.max_lifetime_ms,
                                  Optional ByVal ipv4 As Boolean = _
                                        default_value.ipv4,
                                  Optional ByVal enable_keepalive As Boolean = _
                                        default_value.enable_keepalive,
                                  Optional ByVal first_keepalive_ms As UInt32 = _
                                        default_value.first_keepalive_ms,
                                  Optional ByVal keepalive_interval_ms As UInt32 = _
                                        default_value.keepalive_interval_ms) _
                                 As Boolean
        Return create(token,
                      remote_address,
                      remote_port,
                      ipv4,
                      connecting_timeout_ms,
                      send_rate_sec,
                      response_timeout_ms,
                      receive_rate_sec,
                      max_connecting,
                      max_connected,
                      no_delay,
                      max_lifetime_ms,
                      True,
                      enable_keepalive,
                      first_keepalive_ms,
                      keepalive_interval_ms,
                      r)
    End Function

    Public Shared Function create(ByVal token As String,
                                  ByVal remote_address As String,
                                  ByVal remote_port As UInt16,
                                  Optional ByVal connecting_timeout_ms As Int64 = _
                                        default_value.outgoing.connecting_timeout_ms,
                                  Optional ByVal send_rate_sec As UInt32 = _
                                        default_value.send_rate_sec,
                                  Optional ByVal response_timeout_ms As Int64 = _
                                        default_value.response_timeout_ms,
                                  Optional ByVal receive_rate_sec As UInt32 = _
                                        default_value.receive_rate_sec,
                                  Optional ByVal max_connecting As UInt32 = _
                                        default_value.outgoing.max_connecting,
                                  Optional ByVal max_connected As UInt32 = _
                                        default_value.outgoing.max_connected,
                                  Optional ByVal no_delay As Boolean = _
                                        default_value.no_delay,
                                  Optional ByVal max_lifetime_ms As Int64 = _
                                        default_value.outgoing.max_lifetime_ms,
                                  Optional ByVal ipv4 As Boolean = _
                                        default_value.ipv4,
                                  Optional ByVal enable_keepalive As Boolean = _
                                        default_value.enable_keepalive,
                                  Optional ByVal first_keepalive_ms As UInt32 = _
                                        default_value.first_keepalive_ms,
                                  Optional ByVal keepalive_interval_ms As UInt32 = _
                                        default_value.keepalive_interval_ms) _
                                 As powerpoint
        Dim r As powerpoint = Nothing
        assert(create(token,
                      remote_address,
                      remote_port,
                      r,
                      connecting_timeout_ms,
                      send_rate_sec,
                      response_timeout_ms,
                      receive_rate_sec,
                      max_connecting,
                      max_connected,
                      no_delay,
                      max_lifetime_ms,
                      ipv4,
                      enable_keepalive,
                      first_keepalive_ms,
                      keepalive_interval_ms))
        Return r
    End Function

    Public Shared Function create(ByVal token As String,
                                  ByVal host_or_ip As String,
                                  ByVal port As String,
                                  ByVal ipv4 As String,
                                  ByVal connecting_timeout_ms As String,
                                  ByVal send_rate_sec As String,
                                  ByVal response_timeout_ms As String,
                                  ByVal receive_rate_sec As String,
                                  ByVal max_connecting As String,
                                  ByVal max_connected As String,
                                  ByVal no_delay As String,
                                  ByVal max_lifetime_ms As String,
                                  ByVal is_outgoing As String,
                                  ByVal enable_keepalive As String,
                                  ByVal first_keepalive_ms As String,
                                  ByVal keepalive_interval_ms As String,
                                  ByRef r As powerpoint,
                                  Optional ByVal v As var = Nothing) _
                                 As Boolean
        Dim outgoing As Boolean = False
        outgoing = is_outgoing.to_bool(Not String.IsNullOrEmpty(host_or_ip))
        Return create(token,
                      host_or_ip,
                      port.to_uint16(),
                      ipv4.to_bool(default_value.ipv4),
                      connecting_timeout_ms.to_int64(default_value.outgoing.connecting_timeout_ms),
                      send_rate_sec.to_uint32(default_value.send_rate_sec),
                      response_timeout_ms.to_int64(default_value.response_timeout_ms),
                      receive_rate_sec.to_uint32(default_value.receive_rate_sec),
                      max_connecting.to_uint32(If(outgoing,
                                                  default_value.outgoing.max_connecting,
                                                  default_value.incoming.max_connecting)),
                      max_connected.to_uint32(default_value.outgoing.max_connected),
                      no_delay.to_bool(default_value.no_delay),
                      max_lifetime_ms.to_int64(default_value.outgoing.max_lifetime_ms),
                      outgoing,
                      enable_keepalive.to_bool(default_value.enable_keepalive),
                      first_keepalive_ms.to_uint32(default_value.first_keepalive_ms),
                      keepalive_interval_ms.to_uint32(default_value.keepalive_interval_ms),
                      r,
                      v)
    End Function

    Public Shared Function create(ByVal token As String,
                                  ByVal host_or_ip As String,
                                  ByVal port As String,
                                  ByVal ipv4 As String,
                                  ByVal connecting_timeout_ms As String,
                                  ByVal send_rate_sec As String,
                                  ByVal response_timeout_ms As String,
                                  ByVal receive_rate_sec As String,
                                  ByVal max_connecting As String,
                                  ByVal max_connected As String,
                                  ByVal no_delay As String,
                                  ByVal max_lifetime_ms As String,
                                  ByVal is_outgoing As String,
                                  ByVal enable_keepalive As String,
                                  ByVal first_keepalive_ms As String,
                                  ByVal keepalive_interval_ms As String,
                                  Optional ByVal v As var = Nothing) As powerpoint
        Dim r As powerpoint = Nothing
        assert(create(token,
                      host_or_ip,
                      port,
                      ipv4,
                      connecting_timeout_ms,
                      send_rate_sec,
                      response_timeout_ms,
                      receive_rate_sec,
                      max_connecting,
                      max_connected,
                      no_delay,
                      max_lifetime_ms,
                      is_outgoing,
                      enable_keepalive,
                      first_keepalive_ms,
                      keepalive_interval_ms,
                      r,
                      v))
        Return r
    End Function
End Class
